package com.cyj.redischarts.controller;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@RestController
public class ChartsController {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    public static final String SCORE_RANK = "score_rank";

    @GetMapping("/charts")
    public String getCharts(){
        Set<ZSetOperations.TypedTuple<String>> rangeWithScores = stringRedisTemplate.opsForZSet().reverseRangeWithScores(SCORE_RANK, 0, 10);
        String result = JSON.toJSONString(rangeWithScores);
        return result;
    }
    @PostMapping("/charts")
    public String addCharts(String key,Integer value){
            stringRedisTemplate.opsForZSet().incrementScore(SCORE_RANK,key,1);
            return "ok";
        }
    }



